package com.coin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.UserWallet;
import com.coin.model.R;
import com.coin.service.UserWalletService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Auther: 李 力
 * @Date: 2025/6/7
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@Api(tags = "用户的提币地址")
@RequestMapping("/userWallets")
public class UserWalletController {

    @Autowired
    private UserWalletService userWalletService;

    /**
     * @Description: 分页查询用户的提币地址
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "分页查询用户的提币地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户的id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_wallet_query')")
    public R<Page<UserWallet>> findByPage(@ApiIgnore Page<UserWallet> page, Long userId) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<UserWallet> userWalletPage = userWalletService.findByPage(page, userId);
        return R.ok(userWalletPage);
    }

    /**
     * @Description: 查询用户某种币的提现地址
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @GetMapping("/getCoinAddress/{coinId}")
    @ApiOperation(value = "查询用户某种币的提现地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinId", value = "币种的Id")
    })
    public R<List<UserWallet>> getCoinAddress(@PathVariable("coinId") Long coinId) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<UserWallet> userWallets = userWalletService.findUserWallets(userId, coinId);
        return R.ok(userWallets);
    }


    /**
     * @Description: 新增一个提现地址
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个提现地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userWallet", value = "userWallet json数据")
    })
    public R save(@RequestBody UserWallet userWallet) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        userWallet.setUserId(userId);
        boolean isOk = userWalletService.saveUserWallet(userWallet);
        return isOk ? R.ok() : R.fail("新增失败");
    }

    /**
     * @Description: deleteAddress
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @DeleteMapping("/deleteAddress")
    @ApiOperation(value = "删除某个用户的提现地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressId", value = "提现地址的ID"),
            @ApiImplicitParam(name = "payPassword", value = "交易密码")
    })
    public R delete(@RequestParam(required = true) Long addressId, @RequestParam(required = true) String payPassword) {
        boolean isOk = userWalletService.deleteUserWallet(addressId, payPassword);
        return isOk ? R.ok() : R.fail("删除失败");
    }
}
